#!/bin/bash

echo "go build beginning"

echo "configure go environment variables"
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin

echo "start go module"
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,https://mirrors.aliyun.com/goproxy,https://goproxy.io,direct

echo "prepare directory structure"
JENKINS_DIR=${WORKSPACE}/${MODULE}
echo "jenkins workspace:${JENKINS_DIR}"
#ls ${JENKINS_DIR}


BUILD_DIR=${JENKINS_DIR}/target
echo "build workspace:${BUILD_DIR}"
if [ ! -d ${BUILD_DIR}/${MODULE} ]; then
  mkdir -p ${BUILD_DIR}/${MODULE}
  chmod 777 -R ${BUILD_DIR}
fi

if [ ! -f ${JENKINS_DIR}/config-debug.yaml ]; then
  echo "source config file not fount  ${JENKINS_DIR}/config-debug.yaml"
  exit 1
fi
cp ${JENKINS_DIR}/config-debug.yaml ${BUILD_DIR}/${MODULE}

if [ ! -f ${JENKINS_DIR}/start.sh ]; then
  echo "source start file not fount  ${JENKINS_DIR}/start.sh"
  exit 1
fi
cp ${JENKINS_DIR}/start.sh ${BUILD_DIR}

echo "execute go build, generate executable files"
if [ ! -f ${JENKINS_DIR}/main.go ]; then
  echo "source main file not fount  ${JENKINS_DIR}/main.go"
  exit 1
fi
go build -o ${BUILD_DIR}/${MODULE}_main ${JENKINS_DIR}/main.go

ls ${BUILD_DIR}
ls ${BUILD_DIR}/${MODULE}

echo "go build finished"
